<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 100px;
            height: 100px;
            background: pink;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 让光标在box的中间位置
        //   盒子边界值的判断（box只能在可是窗口移动）
        var box = document.querySelector('.box');
        box.onmousedown = function (e) {
            let xx = e.offsetX;
            let yy = e.offsetY;
            window.onmousemove = function (e) {
                let x = e.clientX - xx;
                let y = e.clientY - yy;


                console.log(window.outerHeight)
                if (x <= 50) {
                    x = 0
                }
                if (x >= window.outerWidth - 100) {
                    x = window.outerWidth - 100
                    console.log(x)
                }
                if (y <= 0) {
                    y = 0
                }
                if (y >= window.innerHeight - 100) {
                    y = window.innerHeight - 100
                }
                box.style.left = x + 'px';
                box.style.top = y + 'px';
            }
        }
        window.onmouseup = function () {
            window.onmousemove = null;
        };
    </script>
</body>

</html>